Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports WiimoteLib

Public Class frmTestConnection

#Region "Form Globals"

    'Container to hold wiimotes
    Private wiimotecollection As WiimoteCollection

    Private LeftSmall As Boolean = True
    Private LeftMedium As Boolean = False
    Private LeftBig As Boolean = False

    Private RightSmall As Boolean = True
    Private RightMedium As Boolean = False
    Private RightBig As Boolean = False

    Private Status1 As Boolean = True
    Private Status2 As Boolean = False
    Private Status3 As Boolean = False

    Private CloseForm As Integer = 0

#End Region

    Private Sub frmTestConnection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        tmrAnimation.Enabled = True

    End Sub

    Private Sub btnTestConnection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestConnection.Click

        tmrProgressBar.Enabled = True

        'BackgroundWorker1.RunWorkerAsync()

        ' Find all wiimotes connected to the system
        wiimotecollection = New WiimoteCollection()
        Dim index As Integer = 1

        Try

            wiimotecollection.FindAllWiimotes()

        Catch ex As WiimoteNotFoundException

            MessageBox.Show(ex.Message, "Wiimote not found error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        Catch ex As WiimoteException

            MessageBox.Show(ex.Message, "Wiimote error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        Catch ex As Exception

            MessageBox.Show(ex.Message, "Unknown error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        End Try

        For Each wiimote As Wiimote In wiimotecollection

            wiimote.Connect()

            wiimote.SetLEDs(index)

            If index = 1 Then
                lblStatus.Text = "Status: One Wiimote Connected"
            Else
                lblStatus.Text = "Status: Two Wiimotes Connected"
            End If

            If wiimote.WiimoteState.ExtensionType <> ExtensionType.BalanceBoard Then
                wiimote.SetReportType(InputReport.IRAccel, True)
            End If

            If wiimote.WiimoteState.ExtensionType = ExtensionType.Nunchuk Then

            End If

            index += 1

        Next wiimote

        ProgressBar.Value = 100
        tmrProgressBar.Enabled = False

        tmrClose.Enabled = True

    End Sub

    Private Sub tmrAnimation_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrAnimation.Tick

        If LeftSmall And RightSmall Then

            pbLeftSmall.Visible = LeftSmall
            pbLeftBig.Visible = LeftBig
            LeftSmall = False
            LeftMedium = True

            pbRightSmall.Visible = RightSmall
            pbRightBig.Visible = RightBig
            RightSmall = False
            RightMedium = True

        ElseIf LeftMedium And RightMedium Then

            pbLeftSmall.Visible = LeftSmall
            pbLeftMedium.Visible = LeftMedium
            LeftMedium = False
            LeftBig = True

            pbRightSmall.Visible = RightSmall
            pbRightMedium.Visible = RightMedium
            RightMedium = False
            RightBig = True

        ElseIf LeftBig And RightBig Then

            pbLeftMedium.Visible = LeftMedium
            pbLeftBig.Visible = LeftBig
            LeftBig = False
            LeftSmall = True

            pbRightMedium.Visible = RightMedium
            pbRightBig.Visible = RightBig
            RightBig = False
            RightSmall = True

        End If

    End Sub

    Private Sub tmrProgressBar_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrProgressBar.Tick

        If ProgressBar.Value >= 100 Then
            ProgressBar.Value = 0
        End If

        If Status1 Then
            lblStatus.Text = "Status: Trying To Connect."
            Status1 = False
            Status2 = True
        ElseIf Status2 Then
            lblStatus.Text = "Status: Trying To Connect.."
            Status2 = False
            Status3 = True
        ElseIf Status3 Then
            lblStatus.Text = "Status: Trying To Connect..."
            Status3 = False
            Status1 = True
        End If

        ProgressBar.PerformStep()

    End Sub

    Private Sub tmrClose_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClose.Tick

        If CloseForm = 1 Then
            tmrAnimation.Enabled = False
            Dim frm As New frmWiiDrums()
            frm.Wiimote_Collection = wiimotecollection
            frm.FormTestConnection = Me
            frm.Show()
            tmrClose.Enabled = False
            Me.Hide()
        End If

        CloseForm += 1

    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        

    End Sub

End Class